//
//  LLTabBar.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/6/25.
//  Copyright © 2016年 任玉林. All rights reserved.
//



import UIKit

//声明协议
protocol LLTabBarDelegate: NSObjectProtocol{
    
    //协议方法
    func  composeButtonClick()
}

class LLTabBar: UITabBar {
    
    //声明代理
    weak var LLDelegate: LLTabBarDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    //super.init(frame:  ) 必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置子控制的frame
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        composeButton.center.x = self.center.x
        
        composeButton.center.y = self.frame.height/2
        
        let width:CGFloat = SCREENW / 5
        
        var index:CGFloat = 0
        
        for view in self.subviews{
            
            
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
               view.frame.size.width = width
                
                view.frame.origin.x = index * width
                
                index++
                
            }
            if index == 2 {
                
                index++
            }
            
        }
        
        
    }
    
    //添加中按钮到到当前视图
    private func setupUI(){
        
        addSubview(composeButton)
    }
    //实现点击方法 中间按钮 用代理方法实现
   @objc private func addButtonClick(){
    
    //调用代理方法
       LLDelegate?.composeButtonClick()
    }

    //懒加载 中间按钮
    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: Selector("addButtonClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //必须写，不然会不显示按钮
        button.sizeToFit()
        
        return button
    }()
}
